<?php
namespace app\api\controller;
use app\Response;
use QL\QueryList;
use \Curl\Curl;
use think\Db;

class Suffix 
{	//修改章节，由“章二”改为“第二章”
	public function index(){
		$path="E:\Project1\Data";
		$suffix = 'rxdata';     //要修改的原后缀名
		$suffix = strtolower($suffix);
		$edit_suffix = 'rvdata2';   //修改后的后缀名

		$file = self::scanFile($path);

		$all_file = count($file);

		$edit_file = 0;

		if($file){
			foreach ($file as $key => $val) {
				$file_Arr = explode('.', $val);
				$count = count($file_Arr);
				$ext = isset($file_Arr[$count-1])?strtolower($file_Arr[$count-1]):''; //后缀名
				if($ext && $ext == $suffix){
					$old_file = $path . '/' . $val;
					$file_Arr[$count-1] = $edit_suffix;  //修改后缀名
					$new_file = $path . '/' . implode('.', $file_Arr);
					$edit = rename($old_file, $new_file);
					if($edit){
						$edit_file++;
					}
				}
			}
		}
		$result = array('all_file'=>$all_file, 'edit_file'=>$edit_file);
		Response::show(200,'ok',$result);
	}

	public static function scanFile($path) {
	  	$result = array();
	  	$files = scandir($path);
	  	if($files){
	  		foreach ($files as $file) {
				if ($file != '.' && $file != '..') {
			  	if (is_dir($path . '/' . $file)) {
					scanFile($path . '/' . $file);
			  	} else {
					$result[] = basename($file);
			  	}
				}
		  	}
		  }	  	
	  	return $result;
	}

}